//判断是否的登录
function goodsDetail_login() {
    var user = document.getElementById("top1_login").innerText;
    var top1 = document.getElementById("top1");
    var container_top = document.getElementById("container_top");
    if (user.trim().length == 0){
        document.getElementById("top1_login").innerText = "登录";
        document.getElementById("top2_gwc").innerText = "0";
        top1.style.width = "140px";
        container_top.style.width = "240px";
        container_top.style.marginLeft = "288px";
    }else if (user.trim().length != 0 && user != "登录"){
        document.getElementById("top1_login").setAttribute("href","#");
        top1.style.width = "195px";
        container_top.style.width = "300px";
        container_top.style.marginLeft = "234px";
    }
}

function shoppingTrolley() {
    var aggregate = 0;
    $(function () {
        $.ajax({
            url: "cartServlet?user_name="+document.getElementById("top1_login").innerText,
            type: "get",
            dataType: "json",
            success: function (res) {
                var shopping_tbody = $("#shopping_tbody");
                shopping_tbody.empty();
                for (var i = 0; i < res.length; i++) {
                    var tr = document.createElement("tr");

                    var td1 = document.createElement("td");
                    td1.setAttribute("height","35px")
                    td1.innerText = i+1;

                    var td2 = document.createElement("td");
                    td2.innerText = res[i].productName;

                    var td3 = document.createElement("td");
                    td3.innerText = res[i].price;

                    var td4 = document.createElement("td");
                    td4.innerText = res[i].quantity;

                    var td5 = document.createElement("td");
                    td5.setAttribute("name","subtotal");
                    td5.innerText = ("￥"+ (res[i].price*res[i].quantity));



                    tr.appendChild(td1);
                    tr.appendChild(td2);
                    tr.appendChild(td3);
                    tr.appendChild(td4);
                    tr.appendChild(td5);
                    shopping_tbody.append(tr);

                    aggregate += res[i].price*res[i].quantity;
                }
                document.getElementById("aggregate").innerText = aggregate;
            },
            error: function (res) {
                alert("所有购物车获取失败！")
            }
        });
    });
}

//判断收货地址
var favg = favg;
function Determine_address() {
    var consigneeName = document.getElementById("consigneeName").value;
    var phone = document.getElementById("phone").value;
    var recordDate = document.getElementById("recordDate").value;
    var hint = document.getElementById("hint");
    var patt = /^((1[3,5,8,7,9][0-9])|(14[5,7])|(17[0,6,7,8])|(19[7]))\d{8}$/;

    if (consigneeName.trim().length == 0 || phone.trim().length == 0 || recordDate.trim().length == 0){
        hint.innerText = "请把收货地址的信息和其他信息认真填写完！！"
        hint.style.color = "red";
    }else if (patt.test(phone)){
        hint.innerText = "  ";
        favg = true;
    }else {
        hint.innerText = "你输入的手机号没有，请重新输入！！"
    }
}

//提交购物车的订单
function shoppin_empty() {
    //获取用户输入的姓名
    var consigneeName = document.getElementById("consigneeName").value;
    //获取用户输入的电话
    var phone = document.getElementById("phone").value;
    //获取用户输入的地址
    var recordDate = document.getElementById("recordDate").value;
    //获取用户账号
    var user_name = document.getElementById("top1_login").innerText;
    //总价格
    var money = document.getElementById("aggregate").innerText;
    var hint = document.getElementById("hint");
    var patt = /^[1][3-9][\d]{9}/;

    if (consigneeName.trim().length == 0 || phone.trim().length == 0 || recordDate.trim().length == 0){
        hint.innerText = "请把收货地址的信息认真填写完！！"
        hint.style.color = "red";
    }else if (patt.test(phone)){
        hint.innerText = "  ";
        var logout = document.getElementById("shopping_bottom_submit");
        logout.onclick=function () {
            var msg = "您确定你输入的收货地址没问题吗？";
            if (confirm(msg)==true){
                window.location.href = "/MyXiaomiShop/order_Add_Servlet?user_name="+user_name+ "&money="+money+ "&consigneeName="+consigneeName+ "&phone="+phone+ "&recordDate="+recordDate;
            }else{
                return false;
            }
        }
    }else {
        hint.innerText = "你输入的手机号没有，请重新输入！！"
    }
}